package com.pompom.workers;

import gummy.database.GummyClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import nl.ansuz.android.steam.dota.request.DotaRequest;
import nl.ansuz.android.steam.dota.response.match.GetMatchHistoryResponse;
import nl.ansuz.android.steam.request.SteamRequest;
import nl.ansuz.android.steam.request.user.GetPlayerSummaries;
import nl.ansuz.android.steam.response.user.GetPlayerSummariesResponse;
import nl.ansuz.android.steam.util.GsonUtil;
import nl.ansuz.android.steam.vo.user.UserVO;

import com.google.gson.Gson;
import com.pompom.matchcrawler.ManipulaHttp2;
import com.pompom.matchcrawler.types.TOAccountWorkQueue;
import com.pompom.matchcrawler.types.TOMatchSkillInfo;
import com.pompom.matchcrawler.types.TOMatchWorkQueue;
import com.pompom.matchcrawler.types.TOPlayerInfo;

public class AccountUpdaterWorker extends AbstractWorker{


 
	private TOAccountWorkQueue[] queue=null; 
	private int fetchSize=10;

	
	private void retrieveNextAccountsFromQueue()
	{
		queue = (TOAccountWorkQueue[]) GummyClient.getInstance("QUEUES").executeQueryTO
				("SELECT * FROM ACCOUNT_WORK_QUEUE LIMIT "+fetchSize, new Object[]{}, TOAccountWorkQueue.class);

		 
	}
	
	public AccountUpdaterWorker()
	{
	 
	}




	public void work()
	{


	 
			retrieveNextAccountsFromQueue();
	 

		if ( queue !=null && queue.length>0)
		{
			String finalList="";
			 for (int i=0;i<queue.length;i++)
			 {
					long steamid = queue[i].getAccountId() +76561197960265728l ;
														
					finalList = finalList+steamid+","; 					
			 }
			 finalList = finalList.substring(0, finalList.length()-1);

			 
				GetPlayerSummaries getPlayerSummaries = new GetPlayerSummaries();
				Map<String, String> params = new HashMap<String, String>();
				params.put(SteamRequest.Parameter.API_KEY, "18755ABB58C0BE96815AE471FBB1C3F2"); 
			    params.put("steamids",finalList );
			    //System.out.println("retrieven players info ...");
			   // System.out.println(finalList);

				String url = getPlayerSummaries.createRequestUrl(DotaRequest.ApplicationID.LIVE, params);						 				
				String resopnse="";
				try {
					resopnse = ManipulaHttp2.executaConsulta(url);
				} catch (IOException e) { 
					e.printStackTrace();
				}
			//	System.out.println(resopnse);				
				GsonUtil gsonUtil = new GsonUtil();		
				GetPlayerSummariesResponse playerSummaries =null;
				Gson gson = new Gson();				
				try{
				 playerSummaries = gson.fromJson(gsonUtil.getStrippedSteamResult(resopnse ), GetPlayerSummariesResponse.class);
				}
				catch (Exception e)
				{ 
					System.out.println("Malformed json a ");
				}
				if (playerSummaries!=null)
				processPlayerSummaries(playerSummaries);
				
		sleepPlus=0;	    
			 
		}
		else
		{
			
			if (sleepPlus==0)
				System.out.println("ACCOUNT_WORK_QUEUE Queue is empty!");
			sleepPlus = 1000;
		}


	}

 


	@Override
	public void setParameters() { 

		 if (getParameter("fetchSize")!=null)
		 {
			 fetchSize = Integer.parseInt(getParameter("fetchSize"));
		 }
	}


	
	 private void processPlayerSummaries(GetPlayerSummariesResponse playerSummaries)
	 {
		 for (int i=0;i<playerSummaries.players.length;i++)
		 {
			 
			 UserVO player = playerSummaries.players[i];
			 TOPlayerInfo toPlayerInfo = new TOPlayerInfo();
			 long accountId = Long.parseLong(player.steamId)-76561197960265728l ; 
			 toPlayerInfo.setAccountId(accountId);
			 toPlayerInfo.setAvatarUrl(player.avatarFull);
			 toPlayerInfo.setPlayerName(player.personaName);
			 try{
			 GummyClient.getInstance("DT2").execute("REPLACE INTO PLAYER_INFO VALUES(?,?,?)",
					 new Object[]{ toPlayerInfo.getAccountId() ,toPlayerInfo.getPlayerName(),toPlayerInfo.getAvatarUrl()});
			 }
			 catch 
			 (Exception e)
			 {
				 e.printStackTrace();
			 }
			 
			 clearAccountFromQueue(accountId);
			 
		 }
	 }

	 
	 private void clearAccountFromQueue(long accountId)
	 {
			GummyClient.getInstance("QUEUES").execute("DELETE FROM ACCOUNT_WORK_QUEUE where ACCOUNTID= ?", new Object[]{""+accountId});
			//System.out.println("DELETE FROM ACCOUNT_WORK_QUEUE where ACCOUNTID= "+accountId);
	 }


}
